package com.daniu101.productbargan.service.impl;

import com.daniu101.productbargan.dao.AdminDao;
import com.daniu101.productbargan.model.Admin;
import com.daniu101.productbargan.model.AdminVo;
import com.daniu101.productbargan.model.VerCode;
import com.daniu101.productbargan.service.IAdminService;
import com.daniu101.productbargan.service.IVercodeService;
import com.daniu101.productbargan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IVercodeService vercodeService;


    @Override
    public String inAdmin(AdminVo adminVo) {
        Admin admin = new Admin();
        //id
        admin.setId(UUID.randomUUID().toString());
        //工号
        admin.setNumber("A"+ StringUtils.getRandom(8));
        //姓名
        admin.setName(adminVo.getName());
        //手机号码
        admin.setPhone(adminVo.getPhone());
        //邮箱
        admin.setEmail(adminVo.getEmail());
        //密码
        admin.setPassword(StringUtils.getMd5(adminVo.getPassword()));
        //对象状态
        admin.setStatus(true);
        //时间
        admin.setDate(new Date());
        int sqlId=adminDao.insertAdmin(admin);
        if (sqlId==1){
            return admin.getId();
        }else{
            return "-1";
        }
    }

    @Override
    public Admin getAdminById(String id) {
        Admin admin = new Admin();
        admin.setId(id);
        return adminDao.selectAdmin(admin);
    }

    @Override
    public Admin getAdminByPhone(String phone) {
        Admin admin = new Admin();
        admin.setPhone(phone);
        return adminDao.selectAdmin(admin);
    }

    @Override
    public Admin getAdminByEmail(String email) {
        Admin admin = new Admin();
        admin.setEmail(email);
        return adminDao.selectAdmin(admin);
    }

    @Override
    public String getAdmins() {

        int count=adminDao.selectAdmins();
        if(count>=1){
            return "0";
        }else{
            return "1";
        }
    }

    @Override
    public String loginVercode(AdminVo adminVo) {
        String phone=adminVo.getPhone();
        Admin admin;
        if(phone.indexOf("@")==-1){
            admin=getAdminByPhone(phone);
        }else{
            admin=getAdminByEmail(phone);
        }
        if(admin ==null){
            return "-1";
        }

        //用户输入的验证码
        String vercode=adminVo.getVercode();
        List<VerCode> vercodes=vercodeService.getVerCode(admin.getId(),true);
        for (VerCode v : vercodes) {
            if(v.getVerCode().equals(vercode)) {

                //设置验证码不在使用
                vercodeService.upVerCode(v);

                return admin.getId();
            }
        }
        return "-1";
    }

    @Override
    public String loginPassword(AdminVo adminVo) {

        // 手机or邮箱？
        String phone = adminVo.getPhone();

        Admin adminDb;
        if (phone.indexOf("@") == -1) {
            // 拿出id 手机
            adminDb = getAdminByPhone(phone);
        } else {
            // 拿出id 邮箱
            adminDb = getAdminByEmail(phone);
        }

        if (adminDb == null){
            return "-1";
        }

        String password = StringUtils.getMd5(adminVo.getPassword());
        String passwordDb = adminDb.getPassword(); // md5

        if (passwordDb.equals(password)){
            // 一致
            return adminDb.getId();
        }

        return "-2";
    }

    @Override
    public String updateAdmin(Admin admin) {
        int rows = adminDao.updateAdmin(admin);
        if (rows ==1 ){
            return "1";
        }else {
            return "-1";
        }
    }
}
